Skip to content

Commit

Permalink
timezoneに対応してみる
Browse files Browse the repository at this point in the history
  • Loading branch information
kawahara committed Aug 19, 2010
1 parent d26afac commit cbf84f2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
5 changes: 4 additions & 1 deletion config/OpenPNE.yml.sample
Expand Up @@ -193,12 +193,15 @@ doctrine_cache_key_prefix: ""
http_proxy: ""

####################################
# 言語設定
# 国際化 (I18n)
####################################

# 対応言語
supported_languages: ['en', 'ja_JP']

# デフォルトタイムゾーン
default_timezone: "Asia/Tokyo"

####################################
# プラグインチャンネルサーバ設定
####################################
Expand Down
36 changes: 35 additions & 1 deletion lib/user/opSecurityUser.class.php
Expand Up @@ -18,9 +18,12 @@
*/
class opSecurityUser extends opAdaptableUser
{
const TIMEZONE_NAMESPACE = 'openpne/user/opSecurityUser/timezone';

protected
$authAdapters = array(),
$serializedMember = '';
$serializedMember = '',
$timezone = null;

/**
* Initializes the current user.
Expand All @@ -37,9 +40,39 @@ public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $o

parent::initialize($dispatcher, $storage, $options);

if (!isset($this->options['timezone']))
{
$this->options['timezone'] = null;
}
if (!isset($this->options['default_timezone']))
{
$this->options['default_timezone'] = sfConfig::get('op_default_timezone', date_default_timezone_get());
}

$currentTimezone = $storage->read(self::TIMEZONE_NAMESPACE);
$this->setTimezone(null !== $this->options['timezone'] ? $this->options['timezone'] : (null !== $currentTimezone ? $currentTimezone : $this->options['default_timezone']));

$this->initializeCredentials();
}

public function shutdown()
{
parent::shutdown();

$this->storage->write(self::TIMEZONE_NAMESPACE, $this->timezone);
}

public function setTimezone($timezone)
{
if ($this->timezone != $timezone)
{
$this->timezone = $timezone;
date_default_timezone_set($timezone);

$this->dispatcher->notify(new sfEvent($this, 'user.change_timezone', array('timezone' => $timezone)));
}
}

public function getMemberId()
{
return $this->getAttribute('member_id', null, 'opSecurityUser');
Expand Down Expand Up @@ -252,6 +285,7 @@ public function login($memberId = null)
}

$this->setCulture($this->getMember()->getConfig('language', sfConfig::get('sf_default_culture')));
$this->setTimezone($this->getMember()->getConfig('time_zone', sfConfig::get('op_default_timezone')));

return $uri;
}
Expand Down
33 changes: 32 additions & 1 deletion lib/util/opDoctrineRecord.class.php
Expand Up @@ -78,13 +78,31 @@ protected function checkIsDatetimeField($fieldName)
return 'datetime' === $definition['type'];
}

protected function checkIsDatetimeOrTimestamp($fieldName)
{
$definition = $this->_table->getColumnDefinition($fieldName);

return ('datetime' === $definition['type'] || 'timestamp' === $definition['type']);
}

protected function _set($fieldName, $value, $load = true)
{
// In setter, empty value must be handled as opDoctrineRecord::UNDEFINED_DATETIME
if ($this->checkIsDatetimeField($fieldName) && empty($value))
{
$value = self::UNDEFINED_DATETIME;
}
elseif ($this->checkIsDatetimeOrTimestamp($fieldName) && $time = strtotime($value))
{
$timezone = date_default_timezone_get();
$defaultTimezone = sfConfig::get('op_default_timezone', 'Asia/Tokyo');
if ($timezone !== $defaultTimezone)
{
date_default_timezone_set($defaultTimezone);
$value = date('Y-m-d H:i:s', $time);
date_default_timezone_set($timezone);
}
}

return parent::_set($fieldName, $value, $load);
}
Expand Down Expand Up @@ -117,7 +135,20 @@ public function _get($fieldName, $load = true)
// In getter, opDoctrineRecord::UNDEFINED_DATETIME must be handled as null
if ($this->checkIsDatetimeField($fieldName) && in_array($value, array(self::UNDEFINED_DATETIME, self::UNDEFINED_DATETIME_BC), true))
{
$value = null;

return null;
}
elseif ($this->checkIsDatetimeOrTimestamp($fieldName) && $value)
{
$timezone = date_default_timezone_get();
$defaultTimezone = sfConfig::get('op_default_timezone', 'Asia/Tokyo');
if ($timezone !== $defaultTimezone)
{
date_default_timezone_set($defaultTimezone);
$time = strtotime($value);
date_default_timezone_set($timezone);
$value = date('Y-m-d H:i:s', $time);
}
}

return $value;
Expand Down

0 comments on commit cbf84f2

Please sign in to comment.